﻿Imports System.Data.SqlClient
Imports System.Data.Sql


Public Class KoneksiDataAkses
    Public conn As SqlConnection
    Public da As SqlDataAdapter
    Public ds As DataSet
    Public cmd As SqlCommand
    Public dt As DataTable

    Public Function open_koneksi()
        'conn = New SqlConnection("server=TOMMY-PC; uid=sa; pwd=tommy351990; database=bisas2;")
        conn = New SqlConnection("Data Source=ABE-WINDOWS8;Initial Catalog=bisas2;Integrated Security=True")
        conn.Open()
        Return conn
    End Function

    Public Function close_koneksi()
        conn.Close()
        Return conn
    End Function

    Public Function binddata(ByVal namatabel As String, ByVal kolom As String)
        open_koneksi()
        da = New SqlDataAdapter("select * from " + namatabel + " order by " + kolom + "", conn)
        ds = New DataSet
        da.Fill(ds)
        close_koneksi()
        Return False
    End Function

    Public Function cekDuplikat(ByVal namatabel As String, ByVal field As String, ByVal kolom As String)
        Dim cnt As Integer
        open_koneksi()
        'da = New SqlDataAdapter("select " + field + " from " + namatabel + " where " + field + " = '" + kolom + "'", conn)
        'da = New SqlDataAdapter("select * from " + namatabel + " where " + field + " = '" + kolom + "' ", conn)
        'ds = New DataSet
        'da.Fill(ds)
        cmd = New SqlCommand("select count(1) from " + namatabel + " where " + field + " = '" + kolom + "' ", conn)
        cnt = CInt(cmd.ExecuteScalar())
        close_koneksi()
        'Return False
        Return cnt
    End Function

    Public Function search(ByVal nama_tabel As String, ByVal field As String, ByVal data As String)
        open_koneksi()
        da = New SqlDataAdapter("select * from " + nama_tabel + " where " + field + " like '%" + data + "%'", conn)
        ds = New DataSet
        da.Fill(ds)
        close_koneksi()
        Return False
    End Function
    Public Sub EksekusiNonQuery(ByVal query As String)
        open_koneksi()
        cmd = New SqlCommand
        cmd.Connection = conn
        cmd.CommandType = CommandType.Text
        cmd.CommandText = query
        cmd.ExecuteNonQuery()
        close_koneksi()
    End Sub

End Class
